#include "rangedialog.h"
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QHBoxLayout>
#include <QVBoxLayout>

RangeDialog::RangeDialog(Range &range, QWidget *parent)
    : QDialog (parent)
{
    // widget
    setWindowTitle("Range");
    setAttribute(Qt::WA_DeleteOnClose, true);
    // controls
    QLabel *lbXMin = new QLabel();
    lbXMin->setText("XMin");
    QLineEdit *leXMin = new QLineEdit();
    leXMin->setText(QString::number(range.xMin));
    QLabel *lbXMax = new QLabel();
    lbXMax->setText("XMax");
    QLineEdit *leXMax = new QLineEdit();
    leXMax->setText(QString::number(range.xMax));
    QLabel *lbYMin = new QLabel();
    lbYMin->setText("YMin");
    QLineEdit *leYMin = new QLineEdit();
    leYMin->setText(QString::number(range.yMin));
    QLabel *lbYMax = new QLabel();
    lbYMax->setText("YMax");
    QLineEdit *leYMax = new QLineEdit();
    leYMax->setText(QString::number(range.yMax));
    QPushButton *pbConfirm = new QPushButton();
    pbConfirm->setText("Confirm");
    QPushButton *pbCancel = new QPushButton();
    pbCancel->setText("Cancel");
    // layout
    QHBoxLayout *hblXMin = new QHBoxLayout();
    hblXMin->addWidget(lbXMin, 1);
    hblXMin->addWidget(leXMin, 1);
    QHBoxLayout *hblXMax = new QHBoxLayout();
    hblXMax->addWidget(lbXMax, 1);
    hblXMax->addWidget(leXMax, 1);
    QHBoxLayout *hblYMin = new QHBoxLayout();
    hblYMin->addWidget(lbYMin, 1);
    hblYMin->addWidget(leYMin, 1);
    QHBoxLayout *hblYMax = new QHBoxLayout();
    hblYMax->addWidget(lbYMax, 1);
    hblYMax->addWidget(leYMax, 1);
    QHBoxLayout *hblButton = new QHBoxLayout();
    hblButton->addWidget(pbConfirm, 1);
    hblButton->addWidget(pbCancel, 1);
    QVBoxLayout *vbl = new QVBoxLayout();
    vbl->addLayout(hblXMin, 1);
    vbl->addLayout(hblXMax, 1);
    vbl->addLayout(hblYMin, 1);
    vbl->addLayout(hblYMax, 1);
    vbl->addLayout(hblButton, 1);
    setLayout(vbl);

    connect(pbConfirm, &QPushButton::clicked, [=](){
        RangeDialog::Range range;
        range.xMin = leXMin->text().toDouble();
        range.xMax = leXMax->text().toDouble();
        range.yMin = leYMin->text().toDouble();
        range.yMax = leYMax->text().toDouble();

        emit setRange(range);

        close();
    });
    connect(pbCancel, &QPushButton::clicked, this, &QWidget::close);
}
